package service

import (
	"context"
	"time"

	"task/driver"
	"task/models"
)

type InitProcessor struct {
}

func (p InitProcessor) Execute(_ context.Context, event *models.ProcessEvent) error {
	var err error
	// 模拟业务处理15ms
	time.Sleep(15 * time.Millisecond)
	// 设置resource的stats1字段
	if err = driver.UpdateResourceStats1(event.ResourceId, 15); err != nil {
		return err
	}
	// 设置状态为完成
	event.ProcessState = models.ProcessStateSuccess
	return nil
}

func (p InitProcessor) Callback(_ context.Context, _ *models.ProcessEvent) error {
	return nil
}

type GrpcProcessor struct {
}

func (p GrpcProcessor) Execute(ctx context.Context, event *models.ProcessEvent) error {
	var (
		result int64
		err    error
	)
	// 调用business的grpc接口获取result设置resource的stats2字段
	if result, err = driver.CallBusiness(ctx, event.ResourceId); err != nil {
		return err
	}
	if err = driver.UpdateResourceStats2(event.ResourceId, result); err != nil {
		return err
	}
	// 设置状态为完成
	event.ProcessState = models.ProcessStateSuccess
	return nil
}

func (p GrpcProcessor) Callback(_ context.Context, _ *models.ProcessEvent) error {
	return nil
}

type Biz1Processor struct {
}

func (p Biz1Processor) Execute(ctx context.Context, event *models.ProcessEvent) error {
	var err error
	// 发送业务消息给业务1
	if err = driver.SendBusiness1EventMsg(ctx, &models.BusinessEvent{
		Event:       models.BusinessEventName,
		ResourceId:  event.ResourceId,
		ProcessorId: event.ProcessorId,
	}); err != nil {
		return err
	}
	// 设置状态为等待
	event.ProcessState = models.ProcessStateWait
	return nil
}

func (p Biz1Processor) Callback(_ context.Context, event *models.ProcessEvent) error {
	var err error
	// 解析回调消息获取结果设置resource的stats3字段
	if err = driver.UpdateResourceStats3(event.ResourceId, event.Result); err != nil {
		return err
	}
	// 设置状态为完成
	event.ProcessState = models.ProcessStateSuccess
	return nil
}

type Biz2Processor struct {
}

func (p Biz2Processor) Execute(ctx context.Context, event *models.ProcessEvent) error {
	var err error
	// 发送业务消息给业务2
	if err = driver.SendBusiness2EventMsg(ctx, &models.BusinessEvent{
		Event:       models.BusinessEventName,
		ResourceId:  event.ResourceId,
		ProcessorId: event.ProcessorId,
	}); err != nil {
		return err
	}
	// 设置状态为等待
	event.ProcessState = models.ProcessStateWait
	return nil
}

func (p Biz2Processor) Callback(_ context.Context, event *models.ProcessEvent) error {
	var err error
	// 解析回调消息获取结果设置resource的stats4字段
	if err = driver.UpdateResourceStats4(event.ResourceId, event.Result); err != nil {
		return err
	}
	// 设置状态为完成
	event.ProcessState = models.ProcessStateSuccess
	return nil
}

type EndProcessor struct {
}

func (p EndProcessor) Execute(_ context.Context, event *models.ProcessEvent) error {
	var err error
	// 模拟业务 耗时等待15ms
	time.Sleep(15 * time.Millisecond)
	// 设置resource的result字段为stats1+stats2+stats3+stats4
	result := event.Resource.Stats1 + event.Resource.Stats2 + event.Resource.Stats3 + event.Resource.Stats4
	if err = driver.UpdateResourceResult(event.ResourceId, result); err != nil {
		return err
	}
	// 设置状态为完成
	event.ProcessState = models.ProcessStateSuccess
	return nil
}

func (p EndProcessor) Callback(_ context.Context, _ *models.ProcessEvent) error {
	return nil
}
